//
// Description: 700. 二叉搜索树中的搜索
// Created by Loading on 2021/11/26.
//

#include <bits/stdc++.h>
#include <Data_Structure_Definition.h>

using namespace std;

TreeNode *searchBST(TreeNode *root, int val) {
    if (!root) {
        return nullptr;
    }
    if (root->val == val) {
        return root;
    }
    TreeNode *node;
    if (val > root->val) {
        node = searchBST(root->right, val);
    } else {
        node = searchBST(root->left, val);
    }

    return node;
}

int main() {
    auto root = new TreeNode(4);
    root->left = new TreeNode(2);
    root->right = new TreeNode(7);
    root->left->left = new TreeNode(1);
    root->left->right = new TreeNode(3);

    int val = 2;
    auto node = searchBST(root, val);
    if (!node) {
        cout << "nullptr" << endl;
    } else {
        cout << node->val << endl;
    }

    return 0;
}
